import { defineStore } from "pinia";
import piniaPersistConfig from "@/stores/helper/persist";
import { userinfoGet } from "@/api/modules/login";
export const useUserStore = defineStore({
  id: "user",
  state: () => ({
    // 登录显示隐藏
    loginVisible: false,
    token: "",
    userId: "",
    // 用户信息
    userInfo: {},
    // 账户信息
    accountInfo: {}
  }),
  getters: {},
  actions: {
    // 显示登录
    showLogin() {
      this.loginVisible = true;
    },
    // 设置 Token
    setToken(token) {
      localStorage.setItem("token", token);
      this.token = token;
    },
    // 设置 userId
    setUserId(userId) {
      localStorage.setItem("userId", userId);
      this.userId = userId;
    },
    // 退出登录
    logout() {
      localStorage.clear();
      this.$reset();
    },
    // 设置用户信息
    setUserInfo(userInfo) {
      localStorage.setItem("userInfo", JSON.stringify(userInfo));
      this.userInfo = userInfo;
    },
    // 获取用户信息
    async getUserInfo() {
      const res = await userinfoGet({
        toUserId: this.userId
      });
      if (res.code === 200) {
        this.setUserInfo(res.data);
      }
    },
    // 获取账户信息
    getAccountInfo() {}
  },
  persist: piniaPersistConfig("user")
});
